//
//  SettingViewController.m
//  ShiZiJieProject
//
//  Created by xiaofang wu on 12-4-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SettingViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "FeedBackViewController.h"
#import "ModifyInfoViewController.h"
#import "ModifySecretViewController.h"
#import "ModifyHeadPicViewController.h"
#import "wxAppDelegate.h"
#import "DataSource.h"
#define kTableViewWidth        280



@interface SettingViewController ()


- (void) initNavigation;
- (void) initSubview;

@end


@implementation SettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
- (void) dealloc
{
    [_tableView release];
    [_loginOffButton release];
    [super dealloc];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initNavigation];
    [self initSubview];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return NO;
}

#pragma 
#pragma mark -- 


- (void) initNavigation
{
//    self.title = @"功能设置";
    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    [tempLabel setText:@"功能设置"];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setTextAlignment:UITextAlignmentCenter];
    [tempLabel setTextColor:[UIColor colorWithRed:111/255.0 green:117/255.0 blue:81/255.0 alpha:1.0]];
    [tempLabel setFont:[UIFont boldSystemFontOfSize:20]];
    self.navigationItem.titleView = tempLabel;
    [tempLabel release];
    tempLabel = nil;
}
- (void) initSubview
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, 416) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.scrollEnabled = NO;
    
//    _tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_texture.png"]];
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_texture.png"]];
    
    _loginOffButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 320, 290, 40)];
   
    [_loginOffButton addTarget:self action:@selector(loginOffButtonPress) forControlEvents:UIControlEventTouchUpInside];
    [_loginOffButton setBackgroundImage:[UIImage imageNamed:@"btn_loginout_normal.png"] forState:UIControlStateNormal];
    [_loginOffButton setBackgroundImage:[UIImage imageNamed:@"btn_loginout_press.png"] forState:UIControlStateHighlighted];
    [_loginOffButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.view addSubview:_loginOffButton];
}






#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int   count  = 0;
    if(section==0)
    {
        count = 3;
    }
    else if(section == 1)
    {
        count = 2;
    }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"UITableViewCell";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    NSString*   string = nil;
    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            string = @"修改个人信息";
        }
        else if(indexPath.row == 1)
        {
            string = @"修改密码";

        }
        else if(indexPath.row == 2)
        {
            string = @"修改头像";

        }
    
    }
    else if(indexPath.section == 1)
    {
        if(indexPath.row == 0)
        {
            string = @"意见反馈";
        }
        else if(indexPath.row == 1)
        {
            string = @"评价一下";
            
        }
    }
    cell.textLabel.text = string;
    cell.textLabel.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString*   string = nil;
    if(section == 0)
    {
            string = @"个人信息:";
    }
    return string;

}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            
            
            ModifyInfoViewController*  temp = [[ModifyInfoViewController alloc] init];
            [self.navigationController pushViewController:temp animated:YES];
            [temp release];

        }
        else if(indexPath.row == 1)
        {
           
            ModifySecretViewController*  temp = [[ModifySecretViewController alloc] init];
            [self.navigationController pushViewController:temp animated:YES];
            [temp release];
            
        }
        else if(indexPath.row == 2)
        {
            
            ModifyHeadPicViewController*  temp = [[ModifyHeadPicViewController alloc] init];
            [self.navigationController pushViewController:temp animated:YES];
            [temp release];
        }
        
    }
    else if(indexPath.section == 1)
    {
        if(indexPath.row == 0)
        {
            FeedBackViewController*  temp = [[FeedBackViewController alloc] init];
            [self.navigationController pushViewController:temp animated:YES];
            [temp release];
        }
        else if(indexPath.row == 1)
        {
        }
    }

}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [[DataSource shareInstance] saveUserStatusToKeyChain:@"" passwd:@""];
        [[wxAppDelegate getAppDelegate] popAllViewController];
        [[wxAppDelegate getAppDelegate] launchFirstView];
    }
}

#pragma 
#pragma mark -- button press
- (void) loginOffButtonPress
{
    UIAlertView* baseAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"下次登录需要重新输入密码，\n确认登出？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认",nil];
    [baseAlert show];
    [baseAlert release];
}
@end
